using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using ThunderFireUITool;
using System;

public class LocalizationSettingWindow : EditorWindow
{
    private static LocalizationSettingWindow c_window;
    private static TextElement localizationFolder_AOT;
    private static TextElement previewTablePath_AOT;
    private static TextElement runtimeTablePath_AOT;


    private static TextElement localizationFolder_HOT;
    private static TextElement previewTablePath_HOT;
    private static TextElement runtimeTablePath_HOT;


    [MenuItem(ThunderFireUIToolConfig.Menu_Localization + "/设置 (Setting)", false, 54)]
    public static void OpenWindow()
    {
        int width = 650;
        int height = 450;
        c_window = GetWindow<LocalizationSettingWindow>();
        c_window.minSize = new Vector2(width, height);
        c_window.position = new Rect((Screen.currentResolution.width - width) / 2, (Screen.currentResolution.height - height) / 2, width, height);
        c_window.titleContent.text = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_设置);
    }

    public static void CloseWindow()
    {
        if (c_window != null)
        {
            c_window.Close();
        }
    }

    private void OnEnable()
    {
        InitWindowData();
        InitWindowUI();
    }

    private VisualElement Root;
    private VisualElement rightContainer;
    private VisualElement leftContainer;
    private Toggle[] toggles;
    private ConfigurationOption LocalizeOption;
    private ConfigurationOption PathOption_AOT;
    private ConfigurationOption PathOption_HOT;

    private void InitWindowData()
    {
        toggles = new Toggle[LocalizationLanguage.Length];
        for (int i = 0; i < toggles.Length; i++)
        {
            toggles[i] = new Toggle();
        }
        foreach (int i in UXGUIConfig.availableLanguages)
        {
            toggles[i].value = true;
        }
    }

    private void InitWindowUI()
    {
        VisualTreeAsset visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(ThunderFireUIToolConfig.UIBuilderPath + "SettingWindow.uxml");
        Root = visualTree.CloneTree();
        rootVisualElement.Add(Root);

        leftContainer = Root.Q<VisualElement>("LeftContainer");
        rightContainer = Root.Q<VisualElement>("RightContainer");

        Label nameLabel = Root.Q<Label>("Title");
        nameLabel.text = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_本地化设置);

        leftContainerRefresh();
        LanguageOnClick();
    }

    private void AddLanguage(string text, int row, int col, ref Toggle toggle)
    {
        toggle.style.position = Position.Absolute;
        toggle.style.left = 50 + 225 * col;
        toggle.style.top = 20 + 30 * row;
        rightContainer.Add(toggle);
        TextElement label = new TextElement();
        label.text = text;
        label.style.position = Position.Absolute;
        label.style.left = 72 + 225 * col;
        label.style.top = 19 + 30 * row;
        label.style.fontSize = 13;
        label.style.color = Color.white;
        rightContainer.Add(label);
    }
    private void LanguageOnClick()
    {
        leftContainerRefresh();
        LocalizeOption.isSelect();
        rightContainer.Clear();
        for (int i = 0; i < LocalizationLanguage.Length; i++)
        {
            AddLanguage(LocalizationLanguage.GetLanguage(i), i / 2, i % 2, ref toggles[i]);
        }

        Button button = EditorUIUtils.CreateUIEButton(EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_保存), null, ConfirmOnClick_Language, 400, 40);
        button.style.top = 200;
        button.style.left = 50;
        rightContainer.Add(button);
    }

    #region 非热更
    private void PathOnClick_AOT()
    {
        int uiTopAdd = -50;

        leftContainerRefresh();
        PathOption_AOT.isSelect();
        rightContainer.Clear();

        TextElement label = new TextElement();
        label.text = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_本地化文件夹_非热更) + ":";
        label.style.position = Position.Absolute;
        label.style.left = 50;
        label.style.top = 20;
        label.style.fontSize = 13;
        label.style.color = Color.white;
        rightContainer.Add(label);

        localizationFolder_AOT = new TextElement();
        localizationFolder_AOT.text = UXGUIConfig.LocalizationFolder_AOT;
        localizationFolder_AOT.style.position = Position.Absolute;
        localizationFolder_AOT.style.left = 50;
        localizationFolder_AOT.style.top = 40;
        localizationFolder_AOT.style.maxWidth = 400;
        localizationFolder_AOT.style.fontSize = 13;
        localizationFolder_AOT.style.color = Color.white;
        rightContainer.Add(localizationFolder_AOT);

        Image mrIcon = new Image();
        mrIcon.style.height = 10;
        mrIcon.image = ToolUtils.GetIcon("More");
        Button MoreBtn = EditorUIUtils.CreateUIEButton("", mrIcon, SelectPath_AOT, 30, 20);
        MoreBtn.style.top = 20;
        MoreBtn.style.right = 50;
        MoreBtn.tooltip = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_选择路径);
        rightContainer.Add(MoreBtn);
        /*
            label = new TextElement();
            label.text = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_静态文本表格路径_非热更) + ":";
            label.style.position = Position.Absolute;
            label.style.left = 50;
            label.style.top = 80;
            label.style.fontSize = 13;
            label.style.color = Color.white;
            rightContainer.Add(label);

            runtimeTablePath_AOT = new TextElement();
            runtimeTablePath_AOT.text = UXGUIConfig.RuntimeTablePath_AOT;
            runtimeTablePath_AOT.style.position = Position.Absolute;
            runtimeTablePath_AOT.style.left = 50;
            runtimeTablePath_AOT.style.top = 100;
            runtimeTablePath_AOT.style.maxWidth = 400;
            runtimeTablePath_AOT.style.fontSize = 13;
            runtimeTablePath_AOT.style.color = Color.white;
            rightContainer.Add(runtimeTablePath_AOT);

            mrIcon = new Image();
            mrIcon.style.height = 10;
            mrIcon.image = ToolUtils.GetIcon("More");
            MoreBtn = EditorUIUtils.CreateUIEButton("", mrIcon, SelectRuntimeFile_AOT, 30, 20);
            MoreBtn.style.top = 80;
            MoreBtn.style.right = 50;
            MoreBtn.tooltip = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_选择路径);
            rightContainer.Add(MoreBtn);
    */
        label = new TextElement();
        label.text = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_动态文本表格路径_非热更) + ":";
        label.style.position = Position.Absolute;
        label.style.left = 50;
        label.style.top = 140 + uiTopAdd;
        label.style.fontSize = 13;
        label.style.color = Color.white;
        rightContainer.Add(label);

        previewTablePath_AOT = new TextElement();
        previewTablePath_AOT.text = UXGUIConfig.PreviewTablePath_AOT;
        previewTablePath_AOT.style.position = Position.Absolute;
        previewTablePath_AOT.style.left = 50;
        previewTablePath_AOT.style.top = 160 + uiTopAdd;
        previewTablePath_AOT.style.maxWidth = 400;
        previewTablePath_AOT.style.fontSize = 13;
        previewTablePath_AOT.style.color = Color.white;
        rightContainer.Add(previewTablePath_AOT);

        mrIcon = new Image();
        mrIcon.style.height = 10;
        mrIcon.image = ToolUtils.GetIcon("More");
        MoreBtn = EditorUIUtils.CreateUIEButton("", mrIcon, SelectPreviewFile_AOT, 30, 20);
        MoreBtn.style.top = 140 + uiTopAdd;
        MoreBtn.style.right = 50;
        MoreBtn.tooltip = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_选择路径);
        rightContainer.Add(MoreBtn);
        /*
                Button button = EditorUIUtils.CreateUIEButton(EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_生成空的静态文本表格_非热更), null, CreateRuntimeTextTable_AOT, 400, 40);
                button.style.top = 200;
                button.style.left = 50;
                rightContainer.Add(button);
        */
        Button button = EditorUIUtils.CreateUIEButton(EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_生成空的动态文本表格_非热更), null, CreatePreviewTextTable_AOT, 400, 40);
        button.style.top = 240 + uiTopAdd;
        button.style.left = 50;
        rightContainer.Add(button);

        button = EditorUIUtils.CreateUIEButton(EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_保存), null, ConfirmOnClick_AOT, 200, 40);
        button.style.top = 300 + uiTopAdd;
        button.style.left = 150;
        rightContainer.Add(button);
    }


    private void CreateRuntimeTextTable_AOT()
    {
        UnityEngine.UI.UXTextTable.CreateRuntimeTable(UXGUIConfig.RuntimeTablePath_AOT, true);
        runtimeTablePath_AOT.text = UXGUIConfig.RuntimeTablePath_AOT;
    }

    private void CreatePreviewTextTable_AOT()
    {
        UnityEngine.UI.UXTextTable.CreatePreviewTable(UXGUIConfig.PreviewTablePath_AOT, true);
        previewTablePath_AOT.text = UXGUIConfig.PreviewTablePath_AOT;
    }


    private void SelectPath_AOT()
    {
        localizationFolder_AOT.text = Utils.SelectFolder() ?? localizationFolder_AOT.text;
    }

    private void SelectRuntimeFile_AOT()
    {
        runtimeTablePath_AOT.text = Utils.SelectFile() ?? runtimeTablePath_AOT.text;
    }

    private void SelectPreviewFile_AOT()
    {
        previewTablePath_AOT.text = Utils.SelectFile() ?? previewTablePath_AOT.text;
    }

    private void ConfirmOnClick_AOT()
    {
        if (localizationFolder_AOT != null)
        {
            UXGUIConfig.LocalizationFolder_AOT = localizationFolder_AOT.text;
            //UXGUIConfig.RuntimeTablePath_AOT = runtimeTablePath_AOT.text;
            UXGUIConfig.PreviewTablePath_AOT = previewTablePath_AOT.text;
        }
        else
        {
            var uxConfig = ResourceManager.Load<UXGUIConfig>("UXGUIConfig");
            EditorUtility.SetDirty(uxConfig);
            AssetDatabase.SaveAssets();
        }
        Selection.activeGameObject = null;
    }

    #endregion

    #region 热更

    private void PathOnClick_HOT()
    {
        int uiTopAdd = -50;

        leftContainerRefresh();
        PathOption_HOT.isSelect();
        rightContainer.Clear();

        TextElement label = new TextElement();
        label.text = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_本地化文件夹_热更) + ":";
        label.style.position = Position.Absolute;
        label.style.left = 50;
        label.style.top = 20;
        label.style.fontSize = 13;
        label.style.color = Color.white;
        rightContainer.Add(label);

        localizationFolder_HOT = new TextElement();
        localizationFolder_HOT.text = UXGUIConfig.LocalizationFolder_HOT;
        localizationFolder_HOT.style.position = Position.Absolute;
        localizationFolder_HOT.style.left = 50;
        localizationFolder_HOT.style.top = 40;
        localizationFolder_HOT.style.maxWidth = 400;
        localizationFolder_HOT.style.fontSize = 13;
        localizationFolder_HOT.style.color = Color.white;
        rightContainer.Add(localizationFolder_HOT);

        Image mrIcon = new Image();
        mrIcon.style.height = 10;
        mrIcon.image = ToolUtils.GetIcon("More");
        Button MoreBtn = EditorUIUtils.CreateUIEButton("", mrIcon, SelectPath_HOT, 30, 20);
        MoreBtn.style.top = 20;
        MoreBtn.style.right = 50;
        MoreBtn.tooltip = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_选择路径);
        rightContainer.Add(MoreBtn);
        /*
                label = new TextElement();
                label.text = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_静态文本表格路径_热更) + ":";
                label.style.position = Position.Absolute;
                label.style.left = 50;
                label.style.top = 80;
                label.style.fontSize = 13;
                label.style.color = Color.white;
                rightContainer.Add(label);

                runtimeTablePath_HOT = new TextElement();
                runtimeTablePath_HOT.text = UXGUIConfig.RuntimeTablePath_HOT;
                runtimeTablePath_HOT.style.position = Position.Absolute;
                runtimeTablePath_HOT.style.left = 50;
                runtimeTablePath_HOT.style.top = 100;
                runtimeTablePath_HOT.style.maxWidth = 400;
                runtimeTablePath_HOT.style.fontSize = 13;
                runtimeTablePath_HOT.style.color = Color.white;
                rightContainer.Add(runtimeTablePath_HOT);

                mrIcon = new Image();
                mrIcon.style.height = 10;
                mrIcon.image = ToolUtils.GetIcon("More");
                MoreBtn = EditorUIUtils.CreateUIEButton("", mrIcon, SelectRuntimeFile_HOT, 30, 20);
                MoreBtn.style.top = 80;
                MoreBtn.style.right = 50;
                MoreBtn.tooltip = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_选择路径);
                rightContainer.Add(MoreBtn);
        */
        label = new TextElement();
        label.text = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_动态文本表格路径_热更) + ":";
        label.style.position = Position.Absolute;
        label.style.left = 50;
        label.style.top = 140 + uiTopAdd;
        label.style.fontSize = 13;
        label.style.color = Color.white;
        rightContainer.Add(label);

        previewTablePath_HOT = new TextElement();
        previewTablePath_HOT.text = UXGUIConfig.PreviewTablePath_HOT;
        previewTablePath_HOT.style.position = Position.Absolute;
        previewTablePath_HOT.style.left = 50;
        previewTablePath_HOT.style.top = 160 + uiTopAdd;
        previewTablePath_HOT.style.maxWidth = 400;
        previewTablePath_HOT.style.fontSize = 13;
        previewTablePath_HOT.style.color = Color.white;
        rightContainer.Add(previewTablePath_HOT);

        mrIcon = new Image();
        mrIcon.style.height = 10;
        mrIcon.image = ToolUtils.GetIcon("More");
        MoreBtn = EditorUIUtils.CreateUIEButton("", mrIcon, SelectPreviewFile_HOT, 30, 20);
        MoreBtn.style.top = 140 + uiTopAdd;
        MoreBtn.style.right = 50;
        MoreBtn.tooltip = EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_选择路径);
        rightContainer.Add(MoreBtn);
        /*
                Button button = EditorUIUtils.CreateUIEButton(EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_生成空的静态文本表格_热更), null, CreateRuntimeTextTable_HOT, 400, 40);
                button.style.top = 200;
                button.style.left = 50;
                rightContainer.Add(button);
        */
        Button button = EditorUIUtils.CreateUIEButton(EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_生成空的动态文本表格_热更), null, CreatePreviewTextTable_HOT, 400, 40);
        button.style.top = 240 + uiTopAdd;
        button.style.left = 50;
        rightContainer.Add(button);

        button = EditorUIUtils.CreateUIEButton(EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_保存), null, ConfirmOnClick_HOT, 200, 40);
        button.style.top = 300 + uiTopAdd;
        button.style.left = 150;
        rightContainer.Add(button);
    }


    private void CreateRuntimeTextTable_HOT()
    {
        UnityEngine.UI.UXTextTable.CreateRuntimeTable(UXGUIConfig.RuntimeTablePath_HOT, true);
        runtimeTablePath_HOT.text = UXGUIConfig.RuntimeTablePath_HOT;
    }

    private void CreatePreviewTextTable_HOT()
    {
        UnityEngine.UI.UXTextTable.CreatePreviewTable(UXGUIConfig.PreviewTablePath_HOT, true);
        previewTablePath_HOT.text = UXGUIConfig.PreviewTablePath_HOT;
    }

    private void SelectPath_HOT()
    {
        localizationFolder_HOT.text = Utils.SelectFolder() ?? localizationFolder_HOT.text;
    }

    private void SelectRuntimeFile_HOT()
    {
        runtimeTablePath_HOT.text = Utils.SelectFile() ?? runtimeTablePath_HOT.text;
    }

    private void SelectPreviewFile_HOT()
    {
        previewTablePath_HOT.text = Utils.SelectFile() ?? previewTablePath_HOT.text;
    }

    private void ConfirmOnClick_HOT()
    {
        if (localizationFolder_HOT != null)
        {
            UXGUIConfig.LocalizationFolder_HOT = localizationFolder_HOT.text;
            //UXGUIConfig.RuntimeTablePath_HOT = runtimeTablePath_HOT.text;
            UXGUIConfig.PreviewTablePath_HOT = previewTablePath_HOT.text;
        }
        else
        {
            var uxConfig = ResourceManager.Load<UXGUIConfig>("UXGUIConfig");
            EditorUtility.SetDirty(uxConfig);
            AssetDatabase.SaveAssets();
        }
        Selection.activeGameObject = null;
    }

    #endregion

    private void leftContainerRefresh()
    {
        leftContainer.Clear();

        LocalizeOption = new ConfigurationOption(EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_语言), LanguageOnClick);
        LocalizeOption.style.top = 0;
        leftContainer.Add(LocalizeOption);

        PathOption_AOT = new ConfigurationOption(EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_非热更), PathOnClick_AOT);
        PathOption_AOT.style.top = 40;
        leftContainer.Add(PathOption_AOT);

        PathOption_HOT = new ConfigurationOption(EditorLocalization.GetLocalization(EditorLocalizationStorage.Def_热更), PathOnClick_HOT);
        PathOption_HOT.style.top = 80;
        leftContainer.Add(PathOption_HOT);
    }

    private void ConfirmOnClick_Language()
    {
        UXGUIConfig.availableLanguages.Clear();
        for (int i = 0; i < LocalizationLanguage.Length; i++)
        {
            if (toggles[i].value)
            {
                UXGUIConfig.availableLanguages.Add(i);
            }
        }
    }

}